package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 分页条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(SensitiveDTO dto) {
        //1.参数检测
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.分页检查
        dto.checkParam();

        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        IPage result = this.page(page, queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(), result.getRecords());
        return pageResponseResult;

    }

    /**
     * 添加敏感词
     *
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive sensitive) {
        // 1 参数校验
        if (sensitive == null || StringUtils.isBlank(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断该敏感词在数据库是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }
        //3 执行新增
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        //4.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1 参数校验
        if (adSensitive == null || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 执行修改
        AdSensitive sensitive = this.getById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //3. 校验名称唯一性
        if (StringUtils.isNotBlank(sensitive.getSensitives()) || !sensitive.getSensitives().equals(adSensitive.getSensitives())) {
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
            }
        }
        this.updateById(adSensitive);

        //4 返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sensitive = this.getById(id);
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 执行删除
        this.removeById(id);
        // 返回结果
        return ResponseResult.okResult();
    }

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    /**
     * 查找所有敏感词
     * @return
     */
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }


}
